package com.cjm.lecode;

import java.util.Arrays;

public class FindMaxConsecutiveOnes {
    public static int findMaxConsecutiveOnes(int[] nums) {
        return method1(nums);
    }
    private static int method2(int[] nums) {
        int max = 0;int temp= 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 1) {
                temp ++;
                if (i == nums.length - 1) {
                    max = max - temp > 0 ? max : temp;
                }
            } else {
                max = max - temp > 0 ? max : temp;
                temp = 0;
            }
        }
        return max;
    }
    private static int method1(int[] nums) {
        int[] result = new int[nums.length];
        int j = 0;
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 1) {
                sum++;
                if (i == nums.length - 1) {
                    result[j] = sum;
                }
                continue;
            }
            if (sum > 0) {
                result[j] = sum;
                j++;
            }
            sum = 0;
        }
        Arrays.sort(result);
        return result[result.length - 1];
    }

    public static void main(String[] args) {
        System.err.println(findMaxConsecutiveOnes(new int[] {1, 1, 0, 1, 1, 1}));
        System.err.println(findMaxConsecutiveOnes(new int[] {1, 0, 1, 1, 0, 1}));
    }
}
